from OWRpy import *
import os.path, redREnviron
import redRi18n
_ = redRi18n.get_(package = 'base')
import signals
import redRGUI

class ecoHclustRectangles(OWRpy): 
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        self.data = {}
        self.RFunctionParam_d = ''
        
        # Inputs
        self.inputs.addInput('id0', 'd', signals.base.RList, self.processd) # here should design dissimilarity signal

        # Outputs
        self.outputs.addOutput("rectangles",_("rectangles"), signals.plotting.RPlotAttribute)

        # GUI
        self.RFunctionParam_k = redRGUI.base.spinBox(self.controlArea, label='Number of groups', min = 2,max=30, value = 6 )
        

        redRGUI.base.commitButton(self.bottomAreaRight, "Commit", callback = self.commitFunction)
    def processd(self, data):
        if not self.require_librarys(["stats"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        if data:
            self.RFunctionParam_d=data.getData()
            #self.data = data
            self.commitFunction()
        else:
            self.RFunctionParam_d=''
    def commitFunction(self):
        if unicode(self.RFunctionParam_d) == '': 
            self.status.setText('No data to process')
            return
        
        print 'SPIN BOX: ', self.RFunctionParam_k.value()
        
        newPlotAtt = signals.plotting.RPlotAttribute(self, data = 'rect.hclust('+str(self.RFunctionParam_d)+', k='+str(self.RFunctionParam_k.value())+')')
        #'+unicode()+')'
        self.rSend("rectangles", newPlotAtt)